package com.expdatacloud.controller.iqc;


import com.expdatacloud.annotation.AutoLog;
import com.expdatacloud.dto.ApiResponse;
import com.expdatacloud.entity.IqcDetection;
import com.expdatacloud.service.IqcDetectionService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * IQC-检测表 前端控制器
 * </p>
 *
 * @author jie
 * @since 2019-11-13
 */
@Controller
@RequestMapping("/iqcDetection")
public class IqcDetectionController {

    @Autowired
    private IqcDetectionService detectionService;

    @GetMapping("/list")
    @ResponseBody
    public ApiResponse list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                            IqcDetection iqcDetection, HttpSession session) {
        saveQueryCondition(iqcDetection, session);
        PageHelper.startPage(page, limit);
        PageInfo<IqcDetection> pageInfo = new PageInfo<>(detectionService.list(iqcDetection));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 保存查询条件
     */
    private void saveQueryCondition(IqcDetection iqcDetection, HttpSession session) {
        //返回的时候不清空session，直接查询的时候，文本框有值就查询，否则查询全部
        if ("1".equals(session.getAttribute("flag"))) {
            getSessionSetValue(iqcDetection, session);
        } else {
            cleanSession(iqcDetection, session);
        }
        session.setAttribute("extReceiptForm", iqcDetection.getExtReceiptForm());
        session.setAttribute("shipper", iqcDetection.getShipper());
        session.setAttribute("sku", iqcDetection.getSku());
        session.setAttribute("inspectionCategoryDesc", iqcDetection.getInspectionCategoryDesc());
        session.setAttribute("supplierKey", iqcDetection.getSupplierKey());
        session.setAttribute("supplierName", iqcDetection.getSupplierName());
        session.setAttribute("checkItemFlag", iqcDetection.getCheckItemFlag());

    }

    /**
     * 当搜索条件为空，置空session
     */
    private void cleanSession(IqcDetection iqcDetection, HttpSession session) {
        if (iqcDetection.getExtReceiptForm() == null || "".equals(iqcDetection.getExtReceiptForm())) {
            session.setAttribute("extReceiptForm", "");
        }
        if (iqcDetection.getShipper() == null || "".equals(iqcDetection.getShipper())) {
            session.setAttribute("shipper", "");
        }
        if (iqcDetection.getSku() == null || "".equals(iqcDetection.getSku())) {
            session.setAttribute("sku", "");
        }
        if (iqcDetection.getInspectionCategoryDesc() == null || "".equals(iqcDetection.getInspectionCategoryDesc())) {
            session.setAttribute("inspectionCategoryDesc", "");
        }
        if (iqcDetection.getSupplierKey() == null || "".equals(iqcDetection.getSupplierKey())) {
            session.setAttribute("supplierKey", "");
        }
        if (iqcDetection.getSupplierName() == null || "".equals(iqcDetection.getSupplierName())) {
            session.setAttribute("supplierName", "");
        }
        if (iqcDetection.getCheckItemFlag() == null || "".equals(iqcDetection.getCheckItemFlag())) {
            session.setAttribute("checkItemFlag", "");
        }
    }

    /**
     * 获取session中的搜索条件，赋值给 iqcDetection
     */
    private void getSessionSetValue(IqcDetection iqcDetection, HttpSession session) {
        if (session.getAttribute("extReceiptForm") != null && !"".equals(session.getAttribute("extReceiptForm"))) {
            iqcDetection.setExtReceiptForm(session.getAttribute("extReceiptForm").toString());
        }
        if (session.getAttribute("shipper") != null && !"".equals(session.getAttribute("shipper"))) {
            iqcDetection.setShipper(session.getAttribute("shipper").toString());
        }
        if (session.getAttribute("sku") != null && !"".equals(session.getAttribute("sku"))) {
            iqcDetection.setSku(session.getAttribute("sku").toString());
        }
        if (session.getAttribute("inspectionCategoryDesc") != null && !"".equals(session.getAttribute("inspectionCategoryDesc"))) {
            iqcDetection.setInspectionCategoryDesc(session.getAttribute("inspectionCategoryDesc").toString());
        }
        if (session.getAttribute("supplierKey") != null && !"".equals(session.getAttribute("supplierKey"))) {
            iqcDetection.setSupplierKey(session.getAttribute("supplierKey").toString());
        }
        if (session.getAttribute("supplierName") != null && !"".equals(session.getAttribute("supplierName"))) {
            iqcDetection.setSupplierName(session.getAttribute("supplierName").toString());
        }
        if (session.getAttribute("checkItemFlag") != null && !"".equals(session.getAttribute("checkItemFlag"))) {
            iqcDetection.setCheckItemFlag(session.getAttribute("checkItemFlag").toString());
        }
        session.setAttribute("flag", "");
    }

    @AutoLog(title = "iqc保存")
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ApiResponse update(@RequestBody Object jsonStr, HttpSession session, @SessionAttribute("users") String user) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            IqcDetection iqcCheckItemDetails = mapper.convertValue(jsonStr, IqcDetection.class);
            iqcCheckItemDetails.setQualityStatus("2");
            return ApiResponse.ok(detectionService.update(iqcCheckItemDetails, user));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(e.getMessage());
        }
    }

    @AutoLog(title = "iqc提交")
    @PostMapping(value = "/submit", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ApiResponse submit(@RequestBody Object jsonStr, String checkItemName,
                              HttpSession session, @CookieValue("library") String library,
                              @SessionAttribute("users") String user) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            IqcDetection iqcDetection = mapper.convertValue(jsonStr, IqcDetection.class);
            return ApiResponse.ok(detectionService.submit(iqcDetection, user, checkItemName, library));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(e.getMessage());
        }
    }

    @AutoLog(title = "iqc提交--已质检、未填写检验项目")
    @PostMapping(value = "/submitCheckItem", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ApiResponse submitCheckItem(@RequestBody Object jsonStr, String checkItemName,
                                       @CookieValue("library") String library,
                                       @SessionAttribute("users") String user) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            IqcDetection iqcDetection = mapper.convertValue(jsonStr, IqcDetection.class);
            return ApiResponse.ok (detectionService.submitCheckItem(iqcDetection,user,library,checkItemName));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 设置返回标识
     */
    @GetMapping("/setReturnFlag")
    @ResponseBody
    public void setReturnFlag(String flag, HttpSession session) {
        session.setAttribute("flag", flag);
    }

}

